package views;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import model.Problem;
import model.Solution;
import processing.core.PApplet;

public class VisualizeApplet extends PApplet {
	Problem problem;
	Solution solution1;
	Solution solution2;
	String message = "";
	List<Integer> selectedProcesses = new ArrayList<Integer>();
	int heightfactor = 2;

	enum MachineOrderType {
		LOCATION, NEIGHBORHOOD
	};

	MachineOrderType mot = MachineOrderType.LOCATION;

	public VisualizeApplet(Problem problem, Solution solution1,
			Solution solution2, MachineOrderType mot) {
		this.solution1 = solution1;
		this.solution2 = solution2;
		this.problem = problem;
		this.mot = mot;
	}

	public void setup() {
		int width = problem.P, height = problem.M * heightfactor;
		size(width, height);
		background(0, 0, 0);
	}

	public void draw() {
		background(0, 0, 0);
		if (mousePressed) {
			for (int p_id = 0; p_id < problem.P; p_id++) {
				int m_id = solution1.getMachineIdForProcessId(p_id);
				stroke(255, 255, 0);
				for (int i = 0; i < heightfactor; i++)
					point(p_id, m_id * heightfactor + i);
				text("Initial Solution", 26, 30, 240, 300);
			}
		} else {
			for (int p_id = 0; p_id < problem.P; p_id++) {
				stroke(255, 255, 0);
				// int m_id1 = solution1.getMachineIdForProcessId(p_id);
				// point(p_id, m_id1);
				int m_id2 = solution2.getMachineIdForProcessId(p_id);
				// int f_m_id2 = problem.fakeMachineIdsOrderedByLocation
				// .get(m_id2);
				int f_m_id2 = m_id2;
				for (int i = 0; i < heightfactor; i++)
					point(p_id, f_m_id2 * heightfactor + i);
				text(message, 5, 5, 500, 300);
				stroke(200, 200, 200);
				if (selectedProcesses.contains(p_id)) {
					model.Process proc = problem.getProcesses().get(p_id);
					List<Integer> aList = new CopyOnWriteArrayList<Integer>(
							proc.getEligibleMachines());
					for (Integer m_id : aList) {
						int f_m_id;
						if (mot == MachineOrderType.LOCATION)
							f_m_id = problem.fakeMachineIdsOrderedByLocation
									.indexOf(m_id);
						else
							f_m_id = problem.fakeMachineIdsOrderedByNeighborhood
									.indexOf(m_id);
						for (int i = 0; i < heightfactor; i++)
							point(p_id, f_m_id * heightfactor + i);
					}
				}
			}
		}
	}

	public void setSolution2(Solution current_solution) {
		solution2 = current_solution;

	}

	public void setMessage(String messageInVisualizer) {
		message = messageInVisualizer;

	}

	public void setSelectedProcesses(List<Integer> selectedProcesses) {
		this.selectedProcesses = selectedProcesses;

	}

}